home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadwin / support / dde.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1995-02-08  |  15.7 KB  |  393 lines

  1. ;                             DDE.LSP
  2. ;
  3. ;  ________________________________________________________________________
  4. ;  
  5. ;  (C) Copyright 1990-1993 by Autodesk, Inc.
  6. ;    by Phil Ford
  7. ;
  8. ;  Permission to use, copy, modify, and distribute this software and its
  9. ;  documentation for any purpose and without fee is hereby granted.  
  10. ;
  11. ;  THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 
  12. ;  ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 
  13. ;  MERCHANTABILITY ARE HEREBY DISCLAIMED.                                
  14. ;  ________________________________________________________________________
  15. ;
  16. ;  DDE.LSP data between AutoCAD and a concurrently running spreadsheet
  17. ;  program using the Dynamic Data Exchange feature of Windows.
  18. ;  This program relies on the ADS program, DDELISP.EXE, to call Windows.
  19. ;
  20. ;
  21. ;---------------------------
  22. ;
  23.  
  24. ;  Main AutoLISP commands:
  25. ;       c:send - send tables and drawing to DDE app
  26. ;       c:get - read data back in and modify any changed entities
  27. ;       sendset - send a selection set
  28. ;
  29. ;  Main DDE functions (implemented with ADS functions in DdeLisp.exe):
  30. ;       ddeprompt - Open channel to default app, put up dialog if failure.
  31. ;                   The DDE dialog saves values in windows\ddelisp.ini.
  32. ;       setddeformat -  AutoCAD Entity filters and layout
  33. ;           0 = all data, use entity names
  34. ;           1 = all data, use handles and not names
  35. ;           2 = decimal handle, handle, tag1, attr1; dec handle, 
  36. ;                               handle, tag2, attr2; ...
  37. ;                       each block insert on one row 
  38. ;           3 = same as 2 but without tags
  39. ;       senddrawing - sends tables, 5 blank lines, rest of drawing
  40. ;       moddrawing - imports data from DDE program and modifies
  41. ;               any drawing entities that have changed
  42. ;       pokealltbl - sends drawing table data to remote app
  43. ;       pokeall - sends drawing entities
  44. ;       pokeset - sends selection set
  45. ;       advise - set up warm link on last item sent
  46. ;       reqmod - requests data and modifies drawing entities
  47. ;               with the data, using a specified range (speed opt)
  48. ;       ddefree  - call after the DDE request data function
  49. ;               "request", to free the shared data transferred from 
  50. ;               a remote application.
  51. ;       ddedone - terminate all DDE channels and free memory
  52.  
  53. ;  Main global variables:
  54. ;       chnl - DDE channel number  (32 allowed)
  55. ;       topic - work file (spreadsheet name)
  56. ;       item - DDE item (cell range description)
  57. ;       row1, col1 - start of spreadsheet data
  58. ;       rowcnt - sum of rows used by last drawing
  59. ;               transmission
  60.  
  61.  
  62.  
  63. ;       Send all the tables and entities to the remote
  64. ;       DDE program (app) on the channel created by ddestart
  65. ;       using "senddrawing" function
  66. (defun c:send ()
  67.   (chkdde)                              ;Check open chnl, prompt user if not
  68.   (unadvise chnl "")                    ;Clear hot link on last item sent
  69.   (sendheading)
  70.   (princ "\nSending drawing data...\n")
  71.   (setq rowcnt (senddrawing row1))
  72.   (advise chnl "" linkcommand)          ;Set up warm link on last 
  73.                                         ;  item sent ("").  When new data comes
  74.                                         ;  in, interrupt AutoCAD and request
  75.                                         ;  the moddrawing function
  76.   (princ "\nNumber of rows used by drawing: ")
  77.   (princ rowcnt)
  78.   (princ "\n")
  79.   (princ)
  80. )
  81.  
  82.  
  83.  
  84. ;       Request data and modify drawing
  85. (defun c:get ()
  86.   (chkdde)                              ;Check open chnl, prompt user if not
  87.   (setq en (entnext))                   ;get first entity name
  88.   (princ "Receiving data and modifying drawing...\n")
  89.   (if (= rowcnt 0)(setq rowcnt 18000))  ;to read in spreadsheet data when
  90.                                         ;  we wrote it in a previous
  91.                                         ;  session, we don't
  92.                                         ;  know the number of rows.  Just
  93.                                         ;  set high limit on read, so  
  94.                                         ;  "moddrawing" will continue until 
  95.                                         ;  end of spreadsheet data.
  96.   (setq modcnt (moddrawing row1 rowcnt))
  97. )
  98.  
  99.  
  100. ;       Send all the tables and then the entities to the remote
  101. ;       DDE program (app) on the channel created by ddestart,
  102. ;       using "pokealltbl" and "pokeall"
  103. (defun sendall ()
  104.   (chkdde)                              ;Check open chnl, prompt user if not
  105.   ;Send all the tables
  106.   (setq tabrows (lenalltbl))            ;in case you need the length before
  107.                                         ;  you send them
  108.   (setq row row1)
  109.   (setq tblrows (pokealltbl chnl row col1)) ;send all the tables
  110.   (setq row (+ row tblrows))            ;set row to end of drawing data in
  111.                                         ;  the spreadsheet
  112.   (setq row (+ row 5))                  ;leave blank lines
  113.   (princ "Rows used by the tables: ")
  114.   (princ tblrows)
  115.   (princ "\n")
  116.  
  117.                                 ;TABLE Definitions
  118.                                 ;VPORT 0
  119.                                 ;LTYPE 1
  120.                                 ;LAYER 2
  121.                                 ;STYLE 3
  122.                                 ;VIEW  4
  123.                                 ;UCS   5
  124.                                 ;BLOCK 6
  125.                                 ;DIMSTYLE 7
  126.  
  127.   (setq blkrows (lentbl 6))     ;6=BLOCK
  128.   (princ "\nNumber of rows in blocks: ")
  129.   (princ blkrows)
  130.   (princ "\n")
  131.  
  132.                         ;Send a table individually--this is just a
  133.                         ;  test.  We already sent all the tables,
  134.                         ;  including the BLOCK table.
  135. ; (setq item (cellstr row col1 rowcnt 4))  ;from row, col, rows sent
  136. ;                                       ;  previously  (4 cols)
  137. ;  (setq rowcnt (poketbl chnl item 6))  ;send BLOCK table (6)
  138. ;                                       ;  to spreadsheet with DDE
  139. ;  (setq row (+ row rowcnt))
  140. ;  (setq row (+ row 1))
  141. ;  (princ "Rows used by the blocks: ")
  142. ;  (princ rowcnt)
  143. ;  (princ "\n")
  144.  
  145.  
  146.   (princ "\nSending drawing data...\n")
  147.   (setq en (entnext))                   ;get first entity name
  148.   (setq rowcnt (pokeall chnl en row col1)) ;send whole drawing to remote
  149.                                         ;  spreadsheet with DDE
  150.   (princ "\nNumber of rows used by entities: ")
  151.   (princ rowcnt)
  152.   (princ "\n")
  153.  
  154.   (princ)
  155. )
  156.  
  157.  
  158. ;       Send a selection set to a remote application, using
  159. ;       "pokeset"
  160. (defun sendset ()
  161.   (chkdde)                              ;Check open chnl, prompt user if not
  162.   (sendheading)
  163.   (setq frmt (getddeformat))
  164.   (if (< frmt 2)
  165.     (setq sset (ssget))                 ;get a selection set of all codes
  166.     (setq sset (ssget "X" (list (cons 0 "ATTRIB"))))
  167.                                         ;just attributes
  168.   )
  169.   (unadvise chnl "")                    ;Clear hot link on last item sent
  170.   (setq rowcnt (pokeset chnl sset row1 col1)) ;send set to remote
  171.                                         ;  spreadsheet with DDE
  172.   (advise chnl "" linkcommand)          ;Set up warm link on last 
  173.                                         ;  item sent ("").  When new data comes
  174.                                         ;  in, interrupt AutoCAD and request
  175.                                         ;  the moddrawing function
  176.   (setq sset nil)
  177.   (princ "Rows: ")
  178.   (princ rowcnt)
  179.   (princ)
  180. )
  181.  
  182.  
  183.  
  184.  
  185. ;       Read DDE data (from DDE remote app)
  186. ;       starting at a certain row, and modify any entities
  187. ;       that have changed.  Allows
  188. ;       column 1 values of less than -4 for comments in
  189. ;       spreadsheet.  -1 is normal entity group code in column 1.
  190. ;       Demonstrates "reqmod" function.
  191. (defun mod ()
  192.   (chkdde)                              ;Check open chnl, prompt user if not
  193.   (setq mrow1 (getint "\nStart row: "))
  194.   (setq nrows (getint "\nNumber of rows: "))
  195.   (setq item (cellstr mrow1 col1 nrows 4))      ;from row, col, rows sent
  196.                                         ;  previously  (4 cols)
  197.   (setq mrowcnt (reqmod chnl item))     ;request range of data, modify
  198.                                         ;  entities that have changed
  199.   (setq entcnt (modcount))
  200.   (ddefree chnl)                        ;free shared data from remote app
  201.   (princ "\nEntities modified: ")
  202.   (princ entcnt)
  203.   (princ "\n")
  204.   (princ "Rows: ")
  205.   (princ mrowcnt)
  206.   (princ)
  207. )
  208.  
  209.  
  210. ;       
  211. ;       
  212. ;               Slower versions, transfer entities one at a time
  213. ;       
  214. ;       
  215.  
  216. ;       Send all the entities in the drawing to the remote app
  217. ;       one at a time.  Demonstates DDE functions
  218. ;       "poke", "request", "execute", and entity transfer function
  219. ;       "entpokecell"
  220. (defun sendall2 ()                      ;Dynamic Data Exchange Sample
  221.                                         ;  Usage
  222.   (chkdde)                              ;Check open chnl, prompt user if not
  223.   (setq cell (cellstr 1 1 1 1))         ;cell item "R1C1", row 1 col 1,
  224.                                         ;  1 number of rows, 1 ncols
  225.  
  226.                 ;Test DDE functions; poke, request, and execute
  227.   (poke chnl cell  
  228.     "AutoCAD / Excel Demo .25...............40.................60.................80") 
  229.                                         ;poke-send data for the cell, check
  230.                                         ;  string length abilities of system
  231.   (setq data (request chnl cell))       ;request-get data from a cell
  232.   (ddefree chnl)                        ;free request data (shared data from
  233.                                         ;  remote app)
  234.   (princ "Data from Spreadsheet: \n")
  235.   (princ data)
  236.   (princ "\n")
  237.   (setq cell (cellstr 1 3 1 1))         ;cell item "R1C1", row 1 col 3
  238.   (setq execstr "[SELECT(\"R1C1\")][FORMULA(\"AutoCAD Graphics Data\")]")
  239.   (execute chnl execstr)
  240.  
  241.  
  242.                         ;Send entities one by one
  243.   (setq en (entnext))                   ;get first entity name
  244.   (setq row row1 col col1)              ;start spread sheet in row 2, col 1
  245.   (setq rowcnt 0)
  246.   (while (not (null en))
  247.     (setq rows (entpokecell chnl en row col))
  248.     (setq rowcnt (+ rowcnt rows))
  249.                                         ;fill cells with entity data list.
  250.                                         ;  rows is the number of items
  251.                                         ;  in list
  252.     (setq row (+ row rows))             ;move down to bottom of that entity
  253.                                         ;  in spreadsheet
  254.     (setq en (entnext en))              ;get next entity name
  255.     (princ "\nNext Available Row ")
  256.     (princ row)
  257.     (princ)
  258.   )
  259.                                         ;just a test--not necessary:
  260.   (setq cell (cellstr row 1 1 1))       ;  create cell string, such as
  261.                                         ;  "R1C1:R2C2", row col nrows ncols
  262.   (poke chnl cell "-99")                ;comment to flag end of sheet
  263.   (princ)
  264. )
  265.  
  266.  
  267. ;       Send a selection set to remote app, one by one.
  268. ;       Demonstates use of entpokecell.
  269. (defun sendset2 ()
  270.   (chkdde)                              ;Check open chnl, prompt user if not
  271.   (setq sset (ssget))                   ;get a selection set
  272.   (setq idx 0)
  273.   (setq en (ssname sset idx))           ;set en to first entity
  274.   (setq row row1 col col1)              ;starting spreadsheet cell
  275.   (setq rowcnt 0)
  276.   (while (not (null en))
  277.     (setq rows (entpokecell chnl en row col))   ;fill cells with entity data
  278.                                         ;  list. rows is the number of items
  279.                                         ;  in list
  280.     (setq row (+ row rows))             ;move down to bottom of that entity
  281.                                         ;  in spreadsheet
  282.     (setq rowcnt (+ rowcnt rows))       
  283.     (setq idx (+ idx 1))
  284.     (setq en (ssname sset idx))         ;set en to idx'th entity
  285.     (princ "\nRow ")
  286.     (princ row)
  287.     (princ)
  288.   )
  289.   (setq sset nil)
  290. )
  291.  
  292.  
  293. ;       Input data from remote app on all the entities in the drawing,
  294. ;       and update any that have changed, with entmod.  Entities
  295. ;       are transferred in sections 100 rows each.
  296. ;       Demonstrates DDE function "reqmod".
  297. (defun mod2 ()                          ;Dynamic Data Exchange Sample
  298.                                         ;  Usage
  299.   (chkdde)                              ;Check open chnl, prompt user if not
  300.   (setq row row1 rows 0 modtot 0)       ;start at top of spreadsheet
  301.   (setq rowtot 0)
  302.   (while (< rowtot rowcnt)              ;while there are more rows to check
  303.                                         ;  based on how many were sent
  304.     (setq item (cellstr row col1 100 4)) ;read in 100 rows at a time
  305.     (princ item)
  306.     (princ " ")
  307.     (setq rows (reqmod chnl item))      ;get data from spreadsheet, and
  308.                                         ;  convert to entities
  309.     (princ rows)
  310.     (princ " ")
  311.     (setq modcnt (modcount))            ;(modcount) gets the last
  312.                                         ;  count value
  313.     (setq modtot (+ modtot modcnt))     ;update modified count.
  314.     (setq row (+ row rows))             ;move down to bottom of that entity
  315.                                         ;  in spreadsheet
  316.     (setq rowtot (+ rowtot rows))
  317.     (princ "Row: ")
  318.     (princ row)
  319.     (princ "\n")
  320.   )
  321.   (princ "\nEntities modified: ")
  322.   (princ modtot)
  323.   (princ)
  324. )
  325.  
  326.  
  327. ;       Make sure we have a DDE channel.  Create one if not.
  328. (defun chkdde ()
  329.   (setq chnl (getddechnl))              ;Ask ddelisp.exe if we have a DDE
  330.                                         ;  channel open
  331.   (if (= 0 chnl) (progn
  332.     (setq chnl (ddeprompt))             ;Try to load app, open channel.  If
  333.                                         ;  fail, put up dialog for app, 
  334.                                         ;  topic, and command line.
  335.     (if (= 0 chnl) (progn
  336.       (princ "No DDE conversation open\n")
  337. ;      (quit)
  338.       ) 
  339.       (setglobals) 
  340.       )
  341.     )
  342.   )
  343. )
  344.  
  345.  
  346. ;       Re-init AutoLISP globals for each new drawing.
  347. (defun setglobals ()
  348.   (setq row1 3 col1 1 rowcnt 0 ddeflag T)
  349.   (fprecision 4 0.01)                   ;Set transfer floating point
  350.                                         ;  precision and fuzz
  351.                                         ;  factor for compares
  352.   (setvar "cmdecho" 0)
  353.   ;(command "_handles" "_on")
  354.   (sethandles)                          ;ADS/DDE function that checks and
  355.                                         ;  sets handles on if they're off.
  356.   (setddeformat 1)                      ;Use handles, not ent names. 
  357.   (setq linkcommand "(moddrawing 3 20000) ")
  358.                                         ;When hot link data comes in, 
  359.                                         ;request row 3 through 20000
  360.                                         ;(or less if spreadsheet has less
  361.                                         ;rows)
  362.   (princ "DDE.LSP and DDELISP.EXE loaded\n")
  363.   (princ)
  364. )
  365.  
  366. ;       Send a title to the spreadsheet
  367. (defun sendheading ()
  368.   (setq cell (cellstr 1 2 1 1))         ;cell item row 1 col 2, 1 row, 1 col
  369.                 ;Test DDE functions; poke, request, and execute
  370.   (poke chnl cell "AutoCAD Drawing Data") 
  371. )
  372.  
  373. ;       To enable (quit) function, to emulate C "break" or "return"
  374. ;(defun *error*(msg)                    ;silent quit from function
  375. ;  (prin1)
  376. ;)
  377.  
  378. ;       Initialize when loading dde.lsp.
  379. (if (null initiate) (progn              ;DDELISP.EXE not loaded.
  380.   (xload "ddelisp")                     ;ADS program that contains the DDE
  381.                                         ;  AutoLISP functions.
  382.   )
  383. )
  384.  
  385. (setq chnl (getddechnl))                ;Ask ddelisp.exe if we have a DDE
  386.                                         ;  channel open
  387. (setq app (getddeapp) topic (getddetopic)) 
  388. (setglobals)
  389.  
  390.  
  391. ;<end of file>
  392.